using System;
using System.IO;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics;
using DevExpress.Utils;
using DevExpress.XtraNavBar;
using System.Runtime.InteropServices;
using DevExpress.Utils.Controls;
using DemoCenter;
using DevExpress.DemoCenter;

namespace DevExpress.Demos.DemoCenter {
    public partial class frmLogin : DevExpress.XtraEditors.XtraForm {
        public enum Platform { Win, ASP, WPF, SL, Frameworks }

        const string startModule = "STARTMODULE_";
        string startProductWin = "";//"XtraBars Suite";
        string startProductASP = "ASPxGridView";
        string startProductWPF = "DXGrid for WPF";
        string startProductSL = "";
        string startProductXAF = "";
        string[] productNames = new string[] {"XtraGrid", "XtraReports", "Xtra2Reports", "Xtra3Reports", "Xtra4Reports", "XtraBars", "XtraScheduler", "XtraRichEdit", "XtraCharts", "Xtra2Charts", "XtraPivotGrid", 
            "XtraLayout", "XtraTreeList", "XtraVerticalGrid", "XtraNavBar", "XtraEditors", "XtraPrintingSystem", "Xpo", "ASPxperience", "ASPxGridView", "ASPxEditors", "ASPxPivotGrid", "XtraSpellChecker", "ASPxScheduler",
			"ASPxHtmlEditor", "ASPxTreeList", "ASPxSpellChecker", "XtraGauges","ASPxGauges", "MVC Extensions", "XtraWizard", "DXGrid", "DXCharts", "DXCarousel", "DXBars", "DXDocking", "DXNavBar", "DXEditors", "DXPrinting", "DXPivotGrid", "DXLayoutControl", "DXControls",
            "AgControls", "AgBars", "AgNavBar", "AgCharts", "AgDocking", "AgGrid", "AgEditors", "AgLayoutControl", "AgRichEdit", "AgScheduler", "AgSpellChecker", "XAF"};
        object[] productDescriptions = new object[] {
			new object[] {"XtraGrid", "XtraGrid.xml", 0, "XtraGrid Suite", Platform.Win}, 
			new object[] {"XtraReports", "XtraReports.xml", 6, "XtraReports Suite", Platform.Win}, 
			new object[] {"Xtra2Reports", "XtraReportsWeb.xml", 25, "XtraReports Suite", Platform.ASP}, 
            new object[] {"Xtra3Reports", "XtraReportsWpf.xml", 46, "XtraReports Suite", Platform.WPF},
            new object[] {"Xtra4Reports", "XtraReportsSL.xml", 46, "XtraReports Suite", Platform.SL},
			new object[] {"XtraBars", "XtraBars.xml", 1, "XtraBars Suite", Platform.Win}, 
			new object[] {"XtraScheduler", "XtraScheduler.xml", 11, "XtraScheduler Suite", Platform.Win}, 
			new object[] {"XtraRichEdit", "XtraRichEdit.xml", 30, "XtraRichEdit Suite", Platform.Win}, 
			new object[] {"XtraCharts", "XtraCharts.xml", 8, "XtraCharts Suite", Platform.Win},
			new object[] {"Xtra2Charts", "XtraChartsWeb.xml", 26, "XtraCharts Suite", Platform.ASP},
			new object[] {"XtraPivotGrid", "XtraPivotGrid.xml", 3, "XtraPivotGrid Suite", Platform.Win},
			new object[] {"XtraLayout", "XtraLayoutControl.xml", 9, "XtraLayoutControl Suite", Platform.Win}, 
			new object[] {"XtraTreeList", "XtraTreeList.xml", 4, "XtraTreeList Suite", Platform.Win}, 
			new object[] {"XtraVerticalGrid", "XtraVerticalGrid.xml", 7, "XtraVerticalGrid Suite", Platform.Win}, 
			new object[] {"XtraNavBar", "XtraNavBar.xml", 2, "XtraNavBar", Platform.Win}, 
			new object[] {"XtraEditors", "XtraEditors.xml", 10, "XtraEditors Library", Platform.Win},  
			new object[] {"XtraPrintingSystem", "XtraPrinting.xml", 5, "XtraPrinting Library", Platform.Win}, 
			new object[] {"XtraSpellChecker", "XtraSpellChecker.xml", 16, "XtraSpellChecker Suite", Platform.Win}, 
            new object[] {"XtraWizard", "XtraWizard.xml", 21, "XtraWizard Control", Platform.Win},

			new object[] {"ASPxGridView", "ASPxGridView.xml", 15, "ASPxGridView Suite", Platform.ASP},
            new object[] {"ASPxEditors", "ASPxEditors.xml", 38, "ASPxEditors Suite", Platform.ASP},
			new object[] {"ASPxScheduler", "ASPxScheduler.xml", 17, "ASPxScheduler Suite", Platform.ASP}, 
			new object[] {"ASPxPivotGrid", "ASPxPivotGrid.xml", 14, "ASPxPivotGrid Suite", Platform.ASP},
			new object[] {"ASPxperience", "ASPxperience.xml", 13, "ASPxperience Suite", Platform.ASP},
			new object[] {"ASPxHtmlEditor", "ASPxHtmlEditor.xml", 20, "ASPxHtmlEditor Suite", Platform.ASP},
			new object[] {"ASPxTreeList", "ASPxTreeList.xml", 18, "ASPxTreeList Suite", Platform.ASP},
			new object[] {"ASPxSpellChecker", "ASPxSpellChecker.xml", 19, "ASPxSpellChecker Suite", Platform.ASP},
            new object[] {"XtraGauges", "XtraGauges.xml", 22, "XtraGauges Suite", Platform.Win},
            new object[] {"ASPxGauges", "ASPxGauges.xml", 27, "ASPxGauges Suite", Platform.ASP},
            new object[] {"MVC Extentions", "MVC.xml", 13, "MVC Extentions", Platform.ASP},
            new object[] {"DXGrid", "DXGrid.xml", 23, "DXGrid for WPF", Platform.WPF},
            new object[] {"DXEditors", "DXEditors.xml", 37, "DXEditors for WPF", Platform.WPF},
            new object[] {"DXCharts", "DXCharts.xml", 24, "DXCharts for WPF", Platform.WPF},
            new object[] {"DXCarousel", "DXCarousel.xml", 28, "DXCarousel for WPF", Platform.WPF},
            new object[] {"DXBars", "DXBars.xml", 35, "DXBars for WPF", Platform.WPF},
            new object[] {"DXDocking", "DXDocking.xml", 34, "DXDocking for WPF", Platform.WPF},
            new object[] {"DXLayoutControl", "DXLayoutControl.xml", 51, "DXLayoutControl for WPF", Platform.WPF},
            new object[] {"DXNavBar", "DXNavBar.xml", 29, "DXNavBar for WPF", Platform.WPF},
            new object[] {"DXPrinting", "DXPrinting.xml", 40, "DXPrinting for WPF", Platform.WPF},
            new object[] {"DXPivotGrid", "DXPivotGrid.xml", 43, "DXPivotGrid for WPF", Platform.WPF},
            new object[] {"DXRichEdit", "DXRichEdit.xml", 30, "DXRichEdit for WPF", Platform.WPF},
            new object[] {"DXSpellChecker", "DXSpellChecker.xml", 16, "DXSpellChecker for WPF", Platform.WPF},
            new object[] {"DXScheduler", "DXScheduler.xml", 11, "DXScheduler for WPF", Platform.WPF},
            new object[] {"DXControls", "DXControls.xml", 53, "DXControls for WPF", Platform.WPF},
            new object[] {"AgControls", "AgControls.xml", 41, "DXControls for Silverlight", Platform.SL},
            new object[] {"AgBars", "AgBars.xml", 48, "DXBars for Silverlight", Platform.SL},
            new object[] {"AgNavBar", "AgNavBar.xml", 49, "DXNavBar for Silverlight", Platform.SL},
            new object[] {"AgCharts", "AgCharts.xml", 52, "DXCharts for Silverlight", Platform.SL},
            new object[] {"AgDocking", "AgDocking.xml", 49, "DXDocking for Silverlight", Platform.SL},
            new object[] {"AgGrid", "AgGrid.xml", 49, "DXGrid for Silverlight", Platform.SL},
            new object[] {"AgEditors", "AgEditors.xml", 39, "DXEditors for Silverlight", Platform.SL},
            new object[] {"AgLayoutControl", "AgLayoutControl.xml", 31, "DXLayoutControl for Silverlight", Platform.SL},
            new object[] {"AgRichEdit", "AgRichEdit.xml", 32, "DXRichEdit fro Silverlight", Platform.SL},
            //new object[] {"AgScheduler", "AgScheduler.xml", 42, "AgScheduler", Platform.SL},
            new object[] {"AgSpellChecker", "AgSpellChecker.xml", 42, "DXSpellChecker for Silverlight", Platform.SL},

			new object[] {"Xpo", "Xpo.xml", 12, "eXpressPersistent Objects", Platform.Frameworks}, 
            new object[] {"XAF", "XAF.xml", 50, "eXpressApp Framework", Platform.Frameworks},
        };

        WebBrowser browser = null;
        Bitmap backgroundBitmap;
        bool blnMoving = false;
        int MouseDownX, MouseDownY;

        public frmLogin(string[] args) {
            InitializeComponent();
            nbcMain1.View = new SkinExplorerBarViewInfoRegistratorEx();
            nbcMain2.View = new SkinExplorerBarViewInfoRegistratorEx();
            nbcMain3.View = new SkinExplorerBarViewInfoRegistratorEx();
            nbcMain4.View = new SkinExplorerBarViewInfoRegistratorEx();
            nbcMain5.View = new SkinExplorerBarViewInfoRegistratorEx();
            //nbcMain1.PaintStyleName = "SkinEx";
            this.BackColor = Color.Magenta;
            backgroundBitmap = Bitmap.FromStream(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("DemoCenter.form-bg.png")) as Bitmap;
            backgroundBitmap.MakeTransparent();
            ClientSize = backgroundBitmap.Size;
            CreateProductList(args.Length > 0 ? args : productNames);
            CreateBrowser();
            CreateImages();
            InitNavigations();
            //this.Icon = ResourceImageHelper.CreateIconFromResources("DemoCenter.demo-center.ico", typeof(frmMain).Assembly);
            this.Opacity = 0;
            if (IsRegionSupported)
                Region = DevExpress.Utils.Win.BitmapToRegion.Convert(backgroundBitmap, Color.Empty);
            else {
                this.TransparencyKey = this.BackColor;
            }
            InitTabGlyphs();
            Timer timer = new Timer();
            timer.Interval = 10;
            timer.Tick += new EventHandler(OnTick);
            timer.Start();
        }

        private void InitTabGlyphs() {
            xtpWin.Image = ResourceImageHelper.CreateImageFromResources(string.Format("DemoCenter.tabglyphs.winforms{0}.png", IsSelectedPage(xtpWin)), typeof(frmMain).Assembly);
            xtpAsp.Image = ResourceImageHelper.CreateImageFromResources(string.Format("DemoCenter.tabglyphs.asp{0}.png", IsSelectedPage(xtpAsp)), typeof(frmMain).Assembly);
            xtpWpf.Image = ResourceImageHelper.CreateImageFromResources(string.Format("DemoCenter.tabglyphs.wpf{0}.png", IsSelectedPage(xtpWpf)), typeof(frmMain).Assembly);
            xtpSL.Image = ResourceImageHelper.CreateImageFromResources(string.Format("DemoCenter.tabglyphs.silverlight{0}.png", IsSelectedPage(xtpSL)), typeof(frmMain).Assembly);
            xtpFrameworks.Image = ResourceImageHelper.CreateImageFromResources(string.Format("DemoCenter.tabglyphs.frameworks{0}.png", IsSelectedPage(xtpFrameworks)), typeof(frmMain).Assembly);
            pictureBox1.Image = ResourceImageHelper.CreateImageFromResources("DemoCenter.logo.png", typeof(frmMain).Assembly);
        }

        string IsSelectedPage(DevExpress.XtraTab.XtraTabPage page) {
            if (xtcMain.SelectedTabPage == page) return "-selected";
            return string.Empty;
        }

        void OnTick(object sender, EventArgs e) {
            if (this.Opacity >= 1) {
                Timer timer = sender as Timer;
                timer.Stop();
            }
            this.Opacity += 0.1;
        }

        bool IsRegionSupported {
            get {
                return false;
                /*if(Environment.OSVersion.Version.Major <= 5 && Environment.OSVersion.Version.Minor <= 0)
                    return false;
                return true;*/
            }
        }

        void CreateProductList(string[] args) {
            foreach (string product in args) {
                object[] obj = GetDescriptionByProduct(product) as object[];
                CreateNavBarItem(obj);
            }
        }
        bool HasVisibleItemLinks(NavBarGroup group) {
            for (int i = 0; i < group.ItemLinks.Count; i++)
                if (group.ItemLinks[i].Visible) return true;
            return false;
        }
        void InitNavigations() {
            nbcMain1.LinkInterval = 2;
            nbcMain2.LinkInterval = 2;
            nbcMain3.LinkInterval = 2;
            nbcMain4.LinkInterval = 2;
            nbcMain5.LinkInterval = 2;

            if (!HasVisibleItemLinks(nbgMain1)) xtpWin.PageVisible = false;
            if (!HasVisibleItemLinks(nbgMain2)) xtpAsp.PageVisible = false;
            if (!HasVisibleItemLinks(nbgMain3)) xtpWpf.PageVisible = false;
            if (!HasVisibleItemLinks(nbgMain4)) xtpSL.PageVisible = false;
            if (!HasVisibleItemLinks(nbgMain5)) xtpFrameworks.PageVisible = false;

            SelectNavBar();
        }
        void CreateImages() {
            ImageCollection images = DevExpress.Utils.Controls.ImageHelper.CreateImageCollectionFromResources("DemoCenter.products.png", typeof(frmMain).Assembly, new Size(16, 16));
            nbcMain1.LargeImages = nbcMain1.SmallImages = nbcMain2.LargeImages = nbcMain2.SmallImages = images;
            nbcMain3.LargeImages = nbcMain3.SmallImages = nbcMain4.LargeImages = nbcMain4.SmallImages = images;
            nbcMain5.LargeImages = nbcMain5.SmallImages = images;
        }

        void CreateNavBarItem(object[] obj) {
            if (obj == null || obj.Length < 3) return;
            NavBarItem item = new NavBarItem();
            ProductDescription pDescription = new ProductDescription(obj);
            item.Tag = pDescription;
            item.LargeImageIndex = item.SmallImageIndex = pDescription.ImageIndex;
            item.Caption = pDescription.Caption;
            item.Visible = pDescription.HtmlFileName != string.Empty;
            switch (pDescription.Platform) {
                case Platform.Win: nbgMain1.ItemLinks.Add(item);
                    return;
                case Platform.ASP: nbgMain2.ItemLinks.Add(item);
                    return;
                case Platform.WPF: nbgMain3.ItemLinks.Add(item);
                    return;
                case Platform.SL: nbgMain4.ItemLinks.Add(item);
                    return;
                default: nbgMain5.ItemLinks.Add(item);
                    return;
            }
        }

        object GetDescriptionByProduct(string name) {
            for (int i = 0; i < productDescriptions.Length; i++) {
                object[] description = productDescriptions.GetValue(i) as object[];
                if (description == null || description.Length < 1) continue;
                if (name.IndexOf(string.Format("{0}", description[0])) > -1) return productDescriptions[i];
            }
            return null;
        }
        void CreateBrowser() {
            browser = IEControlCreator.CreateWebBrowser(panel1);
            string fileName = "about:blank";
            browser.WebMain.Navigating += new WebBrowserNavigatingEventHandler(OnNavigating);
            browser.Navigate(fileName);
        }

        private void nbcMain_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e) {
            ShowModule(e.Link.Item);
            this.ActiveControl = browser;
            browser.ActiveControl = browser.WebMain;
            browser.Focus();
        }

        void ShowModule() {
            if (nbcMain1.Visible && nbgMain1.SelectedLink != null)
                ShowModule(nbgMain1.SelectedLink.Item);
            if (nbcMain2.Visible && nbgMain2.SelectedLink != null)
                ShowModule(nbgMain2.SelectedLink.Item);
            if (nbcMain3.Visible && nbgMain3.SelectedLink != null)
                ShowModule(nbgMain3.SelectedLink.Item);
            if (nbcMain4.Visible && nbgMain4.SelectedLink != null)
                ShowModule(nbgMain4.SelectedLink.Item);
            if (nbcMain5.Visible && nbgMain5.SelectedLink != null)
                ShowModule(nbgMain5.SelectedLink.Item);
        }

        void ShowModule(NavBarItem item) {
            if (item.Tag == null) return;
            string fileName = ((ProductDescription)item.Tag).HtmlFileName;
            if (fileName != string.Empty)
                browser.Navigate(fileName);
        }

        NavBarItemLink GetStartLink(NavBarGroup group, string startProduct) {
            foreach (NavBarItemLink link in group.ItemLinks)
                if (link.Caption == startProduct && link.Item.Visible) return link;
            for (int i = 0; i < group.ItemLinks.Count; i++)
                if (group.ItemLinks[i].Item.Visible)
                    return group.ItemLinks[i];
            return null;
        }

        void SelectStartLink() {
            NavBarItemLink link = GetStartLink(nbgMain1, startProductWin);
            if (link != null) nbcMain1.SelectedLink = link;
            link = GetStartLink(nbgMain2, startProductASP);
            if (link != null) nbcMain2.SelectedLink = link;
            link = GetStartLink(nbgMain3, startProductWPF);
            if (link != null) nbcMain3.SelectedLink = link;
            link = GetStartLink(nbgMain4, startProductSL);
            if (link != null) nbcMain4.SelectedLink = link;
            link = GetStartLink(nbgMain5, startProductXAF);
            if (link != null) nbcMain5.SelectedLink = link;
            ShowModule();
        }

        private void frmMain_Load(object sender, System.EventArgs e) {
            SelectStartLink();
        }

        class ProductDescription {
            string name, htmlFileName, caption;
            int imageIndex;
            Platform platform;
            public ProductDescription(object[] objects)
                : this((string)objects[0],

                "",
                (int)objects[2], objects[3], (Platform)objects[4]) {
                this.htmlFileName = DevExpress.Utils.FilesHelper.FindingFileName(Application.StartupPath, "Data\\" + (string)objects[1], false);
                if (string.IsNullOrEmpty(this.htmlFileName))
                    this.htmlFileName = DevExpress.Utils.FilesHelper.FindingFileName(Application.StartupPath, "..\\eXpressApp Framework\\Data\\" + (string)objects[1], false);
            }
            public ProductDescription(string name, string htmlFileName, int imageIndex, object caption, Platform platform) {
                this.name = name;
                this.htmlFileName = htmlFileName;
                this.imageIndex = imageIndex;
                if (caption == null || string.Empty.Equals(caption))
                    this.caption = name;
                else this.caption = caption.ToString();
                this.platform = platform;
            }

            public string Name { get { return name; } }
            public string HtmlFileName { get { return htmlFileName; } }
            public int ImageIndex { get { return imageIndex; } }
            public string Caption { get { return caption; } }
            public Platform Platform { get { return platform; } }
        }

        void OnNavigating(object sender, WebBrowserNavigatingEventArgs e) {
            int pos = e.Url.ToString().IndexOf(startModule);
            if (pos > 0) {
                StartDemoHelper.StartApplication(e.Url.ToString().Substring(pos + startModule.Length + 1));
                e.Cancel = true;
            }
        }


        private void frmMain_Paint(object sender, System.Windows.Forms.PaintEventArgs e) {
            e.Graphics.DrawImage(backgroundBitmap, 0, 0);
        }

        private void frmMain_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) {
            if (e.Button != MouseButtons.Left) return;
            blnMoving = true;
            MouseDownX = e.X;
            MouseDownY = e.Y;
        }

        private void frmMain_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) {
            if (blnMoving)
                this.Location = new Point(
                    this.Location.X + (e.X - MouseDownX),
                    this.Location.Y + (e.Y - MouseDownY));
        }

        private void frmMain_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) {
            if (e.Button == MouseButtons.Left)
                blnMoving = false;
        }

        private void button1_ButtonClick(object sender, System.EventArgs e) {
            this.Close();
        }

        private void button2_ButtonClick(object sender, System.EventArgs e) {
            this.WindowState = FormWindowState.Minimized;
        }

        /*protected override CreateParams CreateParams {
            get {
                CreateParams cp = base.CreateParams;
                if(IsRegionSupported)
                    cp.ClassStyle |= 0x20000;
                return cp;
            }
        }*/

        void SelectNavBar() {
            panel1.Parent = xtcMain.SelectedTabPage;
            pictureBox1.Parent = xtcMain.SelectedTabPage;
        }

        private void xtcMain_SelectedPageChanged(object sender, DevExpress.XtraTab.TabPageChangedEventArgs e) {
            InitTabGlyphs();
            SelectNavBar();
            ShowModule();
        }

        private void pictureBox1_Click(object sender, EventArgs e) {
            System.Diagnostics.Process.Start("http://www.devexpress.com");
        }
        public static string GetDemoFullPathName(string demoName) {
            return DemosPath + demoName;
        }
        public static string XafDataPath {
            get { return RelativeDemosPath + "..\\eXpressApp Framework\\Data\\"; }
        }
        public static string GetXafDemoFullPathName(string demoName) {
            return DemosPath + "..\\eXpressApp Framework\\" + demoName;
        }
        public static string DemosPath {
            get { return AppDomain.CurrentDomain.BaseDirectory + RelativeDemosPath; }
        }
        static string relativePath = null;
        public static string RelativeDemosPath {
            get {
                if(relativePath != null)
                    return relativePath;
                relativePath = "";
                for(int i = 0; i < 10; i++) {
                    string s = AppDomain.CurrentDomain.BaseDirectory + relativePath + "Data";
                    if(Directory.Exists(s))
                        break;
                    relativePath += "..\\";
                }
                return relativePath;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void hyperLinkEdit1_MouseEnter(object sender, EventArgs e)
        {
            this.hyperLinkEdit1.ForeColor = Color.CornflowerBlue;
        }

        private void hyperLinkEdit1_MouseLeave(object sender, EventArgs e)
        {
            this.hyperLinkEdit1.ForeColor = Color.RoyalBlue;
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }

        private void button2_Click(object sender, EventArgs e)
        {

        }

        private void comboBoxEdit1_MouseEnter(object sender, EventArgs e)
        {
            this.comboBoxEdit1.Properties.Appearance.BorderColor = Color.FromArgb(255, 188, 98);
   
        }

        private void comboBoxEdit1_MouseLeave(object sender, EventArgs e)
        {
            this.comboBoxEdit1.Properties.Appearance.BorderColor = Color.FromArgb(0, 0, 0);
        }

        private void txtEditLoginPwd_MouseEnter(object sender, EventArgs e)
        {
            this.txtEditLoginPwd.Properties.Appearance.BorderColor = Color.FromArgb(255, 188, 98);
        }

        private void txtEditLoginPwd_MouseLeave(object sender, EventArgs e)
        {
            this.txtEditLoginPwd.Properties.Appearance.BorderColor = Color.FromArgb(0, 0, 0);

        }
    }
}
